<template>
	<view>
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
		<view class="padding">
			<button class="bg-green margin-bottom" @click="save" >保存</button>
			<button v-if="index != null" @click="del" class="bg-grey">删除</button>
		</view>
	</view>
</template>

<script>
	import autoformdata from '@/static/json/autoform-data.json'
	import {subOnLoad,formValidation} from '../../../static/js/formUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				initValue:{},
				autoformdata: autoformdata,
				cachename:'',
				index:null,
				datas:[],
				order:0,
				formdata:{}
			}
		},
		methods: {
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if(checkRes){
					this.om.toast(checkRes)
				} else {
					this.om.toast(JSON.stringify(this.formdata))
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					} else {
						this.datas.push(this.formdata)
					}
					uni.setStorage({
					  key: this.cachename,
					  data: this.datas
					})
					uni.navigateBack()
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				Object.assign(_that.initValue, _that.formdata)
				autoformdata[0].order = _that.order
			})
			
		},
	}
</script>

<style>

</style>
